home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 February: Tool Chest / Apple Developer CD Series Tool Chest February 1996 (Apple Computer)(1996).iso / Sample Code / AOCE Sample Code / PowerTalk Access Modules / Sample SMSAM / SampleSMSAM Source / TupleDatabase / TupleDatabase.h < prev    next >
Encoding:
C/C++ Source or Header  |  1995-07-28  |  6.8 KB  |  241 lines  |  [TEXT/MPS ]

  1. /*
  2.     File:        TupleDatabase.h
  3.  
  4.     Copyright:    © 1991-1994 by Apple Computer, Inc.
  5.                 All rights reserved.
  6.  
  7.     Part of the AOCE Sample SMSAM Package.  Consult the license
  8.     which came with this software for your specific legal rights.
  9.  
  10. */
  11.  
  12.  
  13.  
  14. #ifndef __TUPLEDATABASE__
  15. #define __TUPLEDATABASE__
  16.  
  17. #ifndef    __TYPES__
  18. #include "Types.h"
  19. #endif
  20.  
  21. #ifndef    __BUFFER__
  22. #include "Buffer.h"
  23. #endif
  24.  
  25. #ifndef    __DATAITEM__
  26. #include "DataItem.h"
  27. #endif
  28.  
  29. #ifndef    __COMPARABLE__
  30. #include "Comparable.h"
  31. #endif
  32.  
  33. #ifndef    __DIRECTOBJECT__
  34. #include "DirectObject.h"
  35. #endif
  36.  
  37. class ATupleKey;
  38. class ATupleDatabase;
  39.  
  40. /*============================================================================
  41.  
  42.     ATupleDatabase is an abstract protocol for a data base which contains
  43.     arbitrary data records (ADataItem) that are retrieved using arbitrary
  44.     keys (ATupleKey). The database also supports the notion that records
  45.     can be retrived by a integer index from 1..CountTuples().
  46.  
  47.     Subclasses must override all pure virtual methods declared in this class.
  48.  
  49. ----------------------------------------------------------------------------*/
  50.  
  51. class ATupleDatabase : public TDirectObject
  52. {
  53. public:
  54.     virtual ~ATupleDatabase ();
  55.  
  56.     virtual Boolean                 SetTuple ( const ATupleKey&, const ADataItem& ) = 0;
  57.     virtual Boolean                 SetTuple ( unsigned long index, const ADataItem& );
  58.  
  59.     virtual Boolean                 GetTuple ( unsigned long index, ATupleKey&, ADataItem& );
  60.     virtual Boolean                 GetTupleData ( const ATupleKey&, ADataItem& ) = 0;
  61.     virtual Boolean                 GetTupleData ( unsigned long index, ADataItem& );
  62.     virtual Boolean                 GetTupleKey ( unsigned long index, ATupleKey& ) = 0;
  63.     virtual Boolean                 GetTupleDataSize ( const ATupleKey&, unsigned long& ) = 0;
  64.  
  65.     virtual Boolean                 DeleteTuple ( const ATupleKey& ) = 0;
  66.  
  67.     virtual Boolean                 FindIndexOfTuple ( const ATupleKey&, unsigned long& index ) const = 0;
  68.     virtual Boolean                 DoesTupleExist ( const ATupleKey& ) const = 0;
  69.     virtual unsigned long             CountTuples () const = 0;
  70.  
  71.     virtual void                     Flush ();
  72.  
  73.     virtual    ostream&                operator >> ( ostream& ) const;
  74.  
  75. protected:    ATupleDatabase ();
  76.             ATupleDatabase ( const ATupleDatabase& );
  77.             ATupleDatabase&            operator = ( const ATupleDatabase& );
  78. };
  79.  
  80. /*============================================================================
  81.  
  82.     ATupleKey is a class which represents a chunk of data used for a key
  83.     in a tuple database.
  84.  
  85.     Tuple key subclasses MUST NEVER return zero for its data buffer addresses!
  86.     However, it can return a length of zero.
  87.  
  88. ----------------------------------------------------------------------------*/
  89.  
  90. class ATupleKey : public AComparable
  91. {
  92. public:
  93.  
  94.     virtual                            ~ATupleKey ();
  95.  
  96.             ATupleKey&                operator = ( const ATupleKey& );
  97.  
  98.     virtual    const void*                GetData () const = 0;
  99.     virtual    unsigned long            GetLength () const = 0;
  100.  
  101.     virtual    unsigned long            SetLength ( unsigned long ) = 0;
  102.     virtual    unsigned long            SetData ( const StringPtr );
  103.     virtual    unsigned long            SetData ( const char* );
  104.     virtual    unsigned long            SetData ( const void*, unsigned long );
  105.  
  106.     virtual    long                    Compare ( const AComparable& tupleKeysOnly ) const;
  107.     virtual    ostream&                operator >> ( ostream& ) const;
  108.  
  109. protected:    ATupleKey ();
  110.             ATupleKey ( const ATupleKey& );
  111. };
  112.  
  113. /*============================================================================
  114.  
  115.     CTupleKey is a class which represents a tuple key which can
  116.     accept ANY ATupleKey’s data because is contains an internal buffer
  117.     which can be dynamically resized.
  118.  
  119.     Because this class may allocate buffer space on the heap, it is not
  120.     recommended for general use. Use classes which specifically allocate
  121.     statically-sized buffers within their objects instead (i.e. CStringKey)
  122.     because this approach will be more time and space efficient.
  123.  
  124. ----------------------------------------------------------------------------*/
  125.  
  126. class CTupleKey : public ATupleKey
  127. {
  128. public:        CTupleKey ();
  129.             CTupleKey ( unsigned long length );
  130.             CTupleKey ( const char* );
  131.             CTupleKey ( const StringPtr, Boolean includeLengthByte = false );
  132.             CTupleKey ( const void*, unsigned long );
  133.             CTupleKey ( const ADataItem& );
  134.             CTupleKey ( const ATupleKey& );
  135.     virtual    ~CTupleKey ();
  136.  
  137.     virtual    const void*                GetData () const;
  138.     virtual    unsigned long            SetLength ( unsigned long );
  139.     virtual    unsigned long            GetLength () const;
  140.  
  141. protected:    CBuffer                    fBuffer;
  142. };
  143.  
  144. /*============================================================================
  145.  
  146.     CFourByteKey is a statically sized key specifically designed for
  147.     efficiently representing tuple keys which internally use pointers
  148.     or longss as their internal data.
  149.  
  150. ----------------------------------------------------------------------------*/
  151.  
  152. class CFourByteKey : public ATupleKey
  153. {
  154. public:        CFourByteKey ();
  155.             CFourByteKey ( long value );
  156.             CFourByteKey ( unsigned long value );
  157.             CFourByteKey ( const void* value );
  158.     virtual    ~CFourByteKey ();
  159.  
  160.     virtual    const void*                GetData () const;
  161.     virtual    unsigned long            SetLength ( unsigned long );
  162.     virtual    unsigned long            GetLength () const;
  163.     virtual    long                    Compare ( const AComparable& tupleKeysOnly ) const;
  164.  
  165.     virtual    ostream&                operator >> ( ostream& ) const;
  166.  
  167. private:    unsigned long            fData;
  168. };
  169.  
  170. /***********************************|****************************************/
  171. /***********************************|****************************************/
  172.  
  173. #pragma push
  174. #pragma segment ATupleDatabase
  175.  
  176. /***********************************|****************************************/
  177.  
  178. inline unsigned long
  179. ATupleKey::SetData ( const StringPtr string )
  180. {
  181.     return SetData ( string, string [ 0 ] + 1 );
  182. }
  183.  
  184. /***********************************|****************************************/
  185.  
  186. inline ATupleKey&
  187. ATupleKey::operator = ( const ATupleKey& that )
  188. {
  189.     SetData ( that.GetData (), that.GetLength () );
  190.     return *this;
  191. }
  192.  
  193. /***********************************|****************************************/
  194. /***********************************|****************************************/
  195.  
  196. inline const void*
  197. CTupleKey::GetData () const
  198. {
  199.     return fBuffer.GetPhysicalStart ();
  200. }
  201.  
  202. /***********************************|****************************************/
  203.  
  204. inline unsigned long
  205. CTupleKey::GetLength () const
  206. {
  207.     return fBuffer.GetPhysicalLength ();
  208. }
  209.  
  210. /***********************************|****************************************/
  211.  
  212. inline unsigned long
  213. CTupleKey::SetLength ( unsigned long length )
  214. {
  215.     return fBuffer.SetPhysicalLength ( length );
  216. }
  217.  
  218. /***********************************|****************************************/
  219. /***********************************|****************************************/
  220.  
  221. inline unsigned long
  222. CFourByteKey::GetLength () const
  223. {
  224.     return sizeof ( fData );
  225. }
  226.  
  227. /***********************************|****************************************/
  228.  
  229. inline const void*
  230. CFourByteKey::GetData () const
  231. {
  232.     return &fData;
  233. }
  234.  
  235. /***********************************|****************************************/
  236.  
  237. #pragma pop
  238.  
  239. #endif    // __TUPLEDATABASE__
  240.  
  241.